package com.fengdi.everyday.easy;

import java.util.ArrayList;
import java.util.List;


/**
 * @Description 移除字母异位词后的结果数组
 * @Date 08:24
 */
public class RemoveAnagrams {
    public List<String> removeAnagrams(String[] words) {
        ArrayList<String> res = new ArrayList<>();
        res.add(words[0]);
        for(int i = 1; i < words.length; i++) {
            String s1 = words[i - 1];
            String s2 = words[i];
            if(!this.available(s1, s2)) {
                res.add(s2);
            }
        }
        return res;
    }


    // 判断两个单词是否是前一个单词的字母异位词语
    public boolean available(String s1, String s2) {
        if(s1.length() != s2.length()) return false;

        int[] s1Counter = new int[26];
        int[] s2Counter = new int[26];

        for(int i = 0; i < s1.length(); i++) {
            int index = s1.charAt(i) - 'a';
            s1Counter[index] += 1;
        }

        for(int i = 0; i < s2.length(); i++) {
            int index = s2.charAt(i) - 'a';
            s2Counter[index] += 1;
        }

        for(int i = 0; i < 26; i++) {
            if(s1Counter[i] != s2Counter[i]) return false;
        }
        return true;
    }
}
